package com.sen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.MeasureEntity;
import com.sen.service.DruginfoService;
import com.sen.service.MeasureService;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 计量表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("measure")
public class MeasureController {
    @Autowired
    private MeasureService measureService;
    @Autowired
    DruginfoService druginfoService;

    /**
     * 查询所有计量单位
     *
     * @return
     */
    @RequestMapping("/queryAll")
    public R queryAll() {
        List<MeasureEntity> list = measureService.list();
        return R.ok().put("list", list);
    }

    /**
     * 增加和修改计量单位
     *
     * @return
     */
    @RequestMapping("/measureAddOrUpd")
    public void measureAddOrUpd(MeasureEntity measureEntity) {
        measureService.saveOrUpdate(measureEntity);
    }

    /**
     * 删除计量单位
     *
     * @return
     */
    @RequestMapping("/measureDel")
    public R measureDel(String cid) {
        List<DruginfoEntity> drugs = druginfoService.list(new QueryWrapper<DruginfoEntity>().eq("cid", cid));
        if (drugs.size() > 0) {
            return R.error("有药品使用此类型不可删除");
        }
        else{
            measureService.removeById(cid);
            return R.ok();
        }
    }
}
